from django.db import models

# Create your models here.

class Student(models.Model):

    name = models.CharField(max_length=20, verbose_name='姓名')
    age = models.SmallIntegerField(verbose_name='年龄', null=True)
    sex = models.SmallIntegerField(default=1, verbose_name='性别')
    qq = models.CharField(max_length=20, verbose_name='QQ号码', null=True)
    phone = models.CharField(max_length=11, verbose_name='手机号码', null=True)
    c_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') # 自动填充当前时间
    # detail = models.OneToOneField('StudentDetail', on_delete=models.SET_NULL, null=True)
    grade = models.ForeignKey('Grade', on_delete=models.SET_NULL, null=True)

    def __str__(self):

        return self.name

class StudentDetail(models.Model):

    collage = models.CharField(max_length=20, default='', verbose_name='学院名称')
    student = models.OneToOneField('Student', on_delete=models.CASCADE) # 集联删除，为了保持数据一致性，参考表删除，对应的相关对象也删除

class Grade(models.Model):

    name = models.CharField(max_length=20, verbose_name='班级名称')
    num = models.CharField(max_length=20, verbose_name='班期')

    def __str__(self):

        return '{}-{}'.format(self.name, self.num) # 返回只能是字符串

class Course(models.Model):

    name = models.CharField('课程名称', max_length=20)
    student = models.ManyToManyField('Student', through='Enroll')

class Enroll(models.Model):

    student = models.ForeignKey('Student', on_delete=models.CASCADE)
    course = models.ForeignKey('Course', on_delete=models.CASCADE)
    c_time = models.DateTimeField('课程报名时间', auto_now_add=True)
    pay = models.FloatField('缴费金额', default=0)